package com.excu.utils;



import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;


/**
 * @author Xing Dao Rong
 * @since 2021/9/3 17:53
 *  文件工具类
 */
public class FileUtils  implements Serializable {
    private static final long serialVersionUID = 6813374997135795261L;
    /**
     * 创建文件
     * @param path 路径
     * @return 返回
     */
    public static File createFile(String path) {
        return createFile(path, false);
    }
    //新建文件
    private static File newFile;
    /**
     * 创建文件
     * @param path 1
     * @param bool 是否使用懒汉模式
     * @return 返回
     */
    public static File createFile(String path, boolean bool) {
        if (!bool) {
            newFile = new File(path);
        }
        if (Objects.isNull(newFile) && bool) {
            synchronized (Objects.requireNonNull(newFile)) {
                if (Objects.isNull(newFile)) {
                    newFile = new File(path);
                }
            }
        }
        if (!newFile.getParentFile().exists()) {
            newFile.mkdirs();
        }
        return newFile;
    }

    /**
     * 获取文件全路径名称
     * @param filePath 1
     * @return 返回
     */
    public static String getFilePathName(String filePath) {
        String suf = filePath.substring(filePath.indexOf("."));
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        StringBuilder builder = new StringBuilder();
        builder.append(filePath, 0, filePath.indexOf("."));
        int index = 0;
        while (file.exists()) {
            builder.append("(").append(++index).append(")");
            String tempPath = builder.toString();
            filePath = tempPath + suf;
            file = new File(filePath);
        }
        return filePath;
    }


    /**
     * 写入文件
     * @param path 1
     * @param text 1
     */
    public static void writeFile(String path, String text) {
        System.out.println("---------->" + path);
        File file = createFile(path);
        if (!(file.getParentFile()).exists()) {
            file.getParentFile().mkdirs();
        }
        BufferedWriter fw = null;
        try {
            fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, false), StandardCharsets.UTF_8));
            fw.write(text);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    /**
     * 写入文件
     * @param path 1
     * @param text 1
     * @param bool 1
     */
    public static void writeFile(String path, String text, boolean bool) {
        newFile = createFile(path, bool);
        System.out.println("---------->" + path);
        BufferedWriter fw = null;
        try {
            fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, bool), StandardCharsets.UTF_8));
            fw.write(text);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

